package com.newzqxq.koalamoney.presenter;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.newzqxq.koalamoney.common.App;
import com.newzqxq.koalamoney.common.SuperPresenter;
import com.newzqxq.koalamoney.entity.RegisterBean;
import com.newzqxq.koalamoney.model.ForgetPwdModel;
import com.newzqxq.koalamoney.net.Api;
import com.newzqxq.koalamoney.net.DaggerModelComponent;
import com.newzqxq.koalamoney.utils.MyToast;
import com.newzqxq.koalamoney.view.IForgetPwdView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

/**
 * Created by ZQXQ-Developer on 2017/7/26.
 */

public class ForgetPawdPresenter extends SuperPresenter {
    private IForgetPwdView pwdView;
    private Context context;

    @Inject
    ForgetPwdModel model;

    public ForgetPawdPresenter(IForgetPwdView pwdView, Context context) {
        this.pwdView = pwdView;
        this.context = context;
        setupDagger2();
    }

    private void setupDagger2() {
        DaggerModelComponent.builder().appComponent(App.getInstance().getAppComponent()).build().inject(this);
    }


    public void findPawd(String phone, String sms_code, String login_pwd) {
        model.forgetPassword(phone, sms_code, login_pwd);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void findPawd(RespEvent<Void> event) {
        if (event.getUrl().equals(Api.updateUserPassword)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    pwdView.onResult(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        pwdView.onShowToast(event.getExtra());
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    if (event.isAlert()) {
                        pwdView.onShowToast(event.getExtra());
                    }
                    break;
            }
        }
    }


    /**
     * 获取验证码
     *
     * @param phone
     * @param type
     */
    public void getCode(String phone, int type) {
        model.sendCode(phone, type);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCode(RespEvent<String> respEvent) {
        if (respEvent.getUrl().equals(Api.rgGetCode)) {
            switch (respEvent.getCode()) {
                case RespEvent.Code.SUCCEED:
                    break;
                case RespEvent.Code.FAILURE:
                    if (respEvent.isAlert()) {
                        pwdView.onShowToast(respEvent.getExtra());
                    }
                    break;
                case RespEvent.Code.ERROR:
                    pwdView.onShowToast(respEvent.getExtra());
                    break;
            }
        }
    }
}
